package com.lxmes.business.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.lxmes.business.domain.*;
import com.lxmes.business.mapper.CcChukuDetailMapper;
import com.lxmes.business.mapper.CcStockinfoInfoMapper;
import com.lxmes.business.mapper.CcWarehouserecordMapper;
import com.lxmes.common.annotation.DataScope;
import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.NewNumber;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.system.domain.SysProcess;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.business.mapper.CcChukuMainMapper;
import com.lxmes.business.service.ICcChukuMainService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 出库单据主表Service业务层处理
 * 
 * @author 张志煜
 * @date 2022-06-29
 */
@Service
public class CcChukuMainServiceImpl implements ICcChukuMainService 
{
    @Autowired
    private CcChukuMainMapper ccChukuMainMapper;
    @Autowired
    private CcChukuDetailMapper ccChukuDetailMapper;
    @Autowired
    private CcStockinfoInfoMapper stockinfoInfoMapper;
    @Autowired
    private CcWarehouserecordMapper warehouserecordMapper;


    /**
     * 查询出库单据主表
     * 
     * @param id 出库单据主表主键
     * @return 出库单据主表
     */
    @Override
    public CcChukuMain selectCcChukuMainById(Long id)
    {
        return ccChukuMainMapper.selectCcChukuMainById(id);
    }

    @Override
    public CcChukuMain selectCcChukuMainByDanHao(String danhao) {
        return ccChukuMainMapper.selectCcChukuMainByDanHao(danhao);
    }

    @Override
    public List<CcChukuMain> selectChukumainBychukuFlag(String chukuFlag) {
        return ccChukuMainMapper.selectChukumainBychukuFlag(chukuFlag);
    }

    /**
     * 查询出库单据主表列表
     * 
     * @param ccChukuMain 出库单据主表
     * @return 出库单据主表
     */
    @Override
    @DataScope(deptAlias = "de", userAlias = "de")
    public List<CcChukuMain> selectCcChukuMainList(CcChukuMain ccChukuMain)
    {
        return ccChukuMainMapper.selectCcChukuMainList(ccChukuMain);
    }

    @Override
    @Transactional
    public int zhixingChuku(String danhao) {
        CcChukuMain main = ccChukuMainMapper.selectCcChukuMainByDanHao(danhao);
        List<CcChukuDetail> detailList = ccChukuDetailMapper.selectCcChukuDetailByDanHao(danhao);
        if(detailList.size()>0){
            //遍历所有带入库的物料
            for (CcChukuDetail detail:detailList) {
                //查询库存的物料
                CcStockinfoInfo stockInfo = stockinfoInfoMapper.selectCcStockinfoInfoByItemIdAndLot(detail.getItemCode(),detail.getLot());
                //减掉库存数量
                if(stockInfo.getKcNum().compareTo(detail.getCkNum())==1 || stockInfo.getKcNum().compareTo(detail.getCkNum())==0){
                    stockInfo.setUpdateBy(SecurityUtils.getUsername());
                    stockInfo.setUpdateTime(new Date());
                    stockInfo.setKcNum(stockInfo.getKcNum().subtract(detail.getCkNum()));
                    stockInfo.setLockNum(stockInfo.getLockNum().subtract(detail.getCkNum()));
                    stockinfoInfoMapper.updateCcStockinfoInfo(stockInfo);
                }else {
                    throw new ServiceException("库存数量及出库数量错误！");
                }
                //新增入库流水
                CcWarehouserecord warehouserecord=new CcWarehouserecord();
                warehouserecord.setDeptId(SecurityUtils.getDeptId());
                warehouserecord.setItemId(detail.getItemCode());
                warehouserecord.setItemName(detail.getItemName());
                warehouserecord.setBsinssType(0L);
                warehouserecord.setCrNum(detail.getCkNum());
                warehouserecord.setSupplyId(detail.getSupplyId());
                warehouserecord.setSupplyName(detail.getSupplyName());
                warehouserecord.setSpec(detail.getSpec());
                warehouserecord.setUnit(detail.getUnit());
                warehouserecord.setClassId(detail.getClassId());
                warehouserecord.setClassName(detail.getClassName());
                warehouserecord.setCreateBy(SecurityUtils.getUsername());
                warehouserecord.setCreateTime(new Date());
                warehouserecordMapper.insertCcWarehouserecord(warehouserecord);
            }
            main.setState(1L);
            ccChukuMainMapper.updateCcChukuMain(main);
        }else {
            throw new ServiceException("出库单物料不存在！");
        }
        return 1;
    }

    /**
     * 新增出库单据主表
     * 
     * @param ccChukuMain 出库单据主表
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCcChukuMain(CcChukuMain ccChukuMain)
    {
        ccChukuMain.setCreateTime(DateUtils.getNowDate());
        ccChukuMain.setCreateBy(SecurityUtils.getUsername());
        ccChukuMain.setDeptId(SecurityUtils.getDeptId());
        ccChukuMain.setDeptId(SecurityUtils.getDeptId());
        String number= "CK-"+NewNumber.createNewNumber(ccChukuMainMapper.getNewCode(DateUtils.getDate()));
        ccChukuMain.setDanhao(number);
        List<CcChukuDetail> list = ccChukuMain.getInfoList();
        if(list.size()>0){
            for ( CcChukuDetail info:list) {
                info.setCukuDate(ccChukuMain.getCukuDate());
                info.setDanhao(number);
                info.setCreateTime(DateUtils.getNowDate());
                info.setCreateBy(SecurityUtils.getUsername());
                //CcStockinfoInfo stockInfo = stockinfoInfoMapper.selectCcStockinfoInfoByItemId(info.getItemCode());
                CcStockinfoInfo stockInfo = stockinfoInfoMapper.selectCcStockinfoInfoByItemIdAndLot(info.getItemCode(),info.getLot());
                if(stockInfo.getLockNum().add(info.getCkNum()).compareTo(stockInfo.getKcNum())==1){
                    throw new ServiceException(info.getItemCode()+":可用数量已超出库存数量！");
                }
                stockInfo.setLockNum(stockInfo.getLockNum().add(info.getCkNum()));
                stockinfoInfoMapper.updateCcStockinfoInfo(stockInfo);
                ccChukuDetailMapper.insertCcChukuDetail(info);
            }
        }else {
            throw new ServiceException("物料不存在！");
        }
        Map<String, String> map = ccChukuDetailMapper.tJChukuDetail(number);
        ccChukuMain.setJzFlag("未结清");
        ccChukuMain.setTotalNum(new BigDecimal(String.valueOf(map.get("ckNum"))));
        ccChukuMain.setTotalPrice(new BigDecimal(String.valueOf(map.get("ckPrice"))));
        ccChukuMainMapper.insertCcChukuMain(ccChukuMain);
        return 1;
    }

    /**
     * 修改出库单据主表
     * 
     * @param ccChukuMain 出库单据主表
     * @return 结果
     */
    @Override
    public int updateCcChukuMain(CcChukuMain ccChukuMain)
    {
        ccChukuMain.setUpdateTime(DateUtils.getNowDate());
        return ccChukuMainMapper.updateCcChukuMain(ccChukuMain);
    }

    /**
     * 批量删除出库单据主表
     * 
     * @param ids 需要删除的出库单据主表主键
     * @return 结果
     */
    @Override
    public int deleteCcChukuMainByIds(Long[] ids)
    {
        return ccChukuMainMapper.deleteCcChukuMainByIds(ids);
    }

    /**
     * 删除出库单据主表信息
     * 
     * @param id 出库单据主表主键
     * @return 结果
     */
    @Override
    public int deleteCcChukuMainById(Long id)
    {
        return ccChukuMainMapper.deleteCcChukuMainById(id);
    }

    @Override
    @Transactional
    public int deleteCcChukuMainByDanHao(String danhao) {
        List<CcChukuDetail> detailList = ccChukuDetailMapper.selectCcChukuDetailByDanHao(danhao);
        for (CcChukuDetail info:detailList) {
            CcStockinfoInfo stockInfo = stockinfoInfoMapper.selectCcStockinfoInfoByItemIdAndLot(info.getItemCode(),info.getLot());
            stockInfo.setLockNum(stockInfo.getLockNum().subtract(info.getCkNum()));
            stockinfoInfoMapper.updateCcStockinfoInfo(stockInfo);
        }
        ccChukuMainMapper.deleteCcChukuMainByDanHao(danhao);
        ccChukuDetailMapper.deleteCcChukuDetailByDanHao(danhao);
        return 1;
    }
}
